package com.hyfrogx.modules.app.controller.charge;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.exception.ServiceException;
import com.hyfrogx.common.param.JSONResult;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.annotation.Login;
import com.hyfrogx.modules.app.form.HyChargeVO;
import com.hyfrogx.modules.app.form.ScoreByMoneyForm;
import com.hyfrogx.modules.app.form.alipay.AliPayVo;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.charge.entity.HyCharge;
import com.hyfrogx.modules.charge.service.HyChargeService;
import com.hyfrogx.modules.manage.entity.HyProductionScaleEntity;
import com.hyfrogx.modules.manage.entity.chargePayRecord.ChargePayRecordEntity;
import com.hyfrogx.modules.manage.service.HyPayRecordService;
import com.hyfrogx.modules.manage.service.HyProductionVacuateService;
import com.hyfrogx.modules.manage.service.chargePayRecord.ChargePayRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.hyfrogx.modules.manage.controller.BaseController;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 支付记录的调用接口
 *
 * @项目 hyplatform2.0
 * @日期: 2017年8月1日
 * @负责人: Orchid
 */
@RestController
@RequestMapping(value = "/api/charge")
@Api(value = "ApiChargeController", tags = {"客户端--充值模板"})
public class ApiChargeController extends BaseController {

    @Autowired
    ChargePayRecordService chargePayRecordService;
    @Autowired
    HyPayRecordService hyPayRecordService;

    @Autowired
    HyChargeService chargeService;

	@Autowired
	HyChargeService hyChargeService;

	@Autowired
	HyProductionVacuateService hyProductionVacuateService;

    //@Login
    @ApiOperation("客户端充值记录")
    @RequestMapping(value = "alipay/get", method = RequestMethod.GET)
    public  R getChargePayRecord(@RequestParam Map<String, Object> map) {
        try {
            if (map.get("uid").equals("")) {
                return R.error("用户不能为空");
            }
            Page page = chargePayRecordService.getPayRecordList(new QueryExt<>(map));
            return R.ok().put("page",page);
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    @Login
    @ApiOperation("移动端获取苹果充值现金虚拟币比例")
    @RequestMapping(value = "getScale", method = RequestMethod.GET)
    public R getIosCharge(@RequestParam String paramKey) {
       try {
			HyProductionScaleEntity vipScale = hyProductionVacuateService.getOne(new QueryWrapper<HyProductionScaleEntity>().eq(
					"param_key",paramKey));
			if(vipScale!=null){
                return R.ok().put("scale",vipScale.getScale());
            }else{
                return R.ok().put("scale",null);
            }


		} catch (MyException e) {
			return R.error(e.getMessage());
		}
    }


    @Login
    @ApiOperation("获取该档位充值是否含奖励积分及积分")
    @RequestMapping(value = "getScoreByMoney", method = RequestMethod.POST)
    public R getScoreByMoney(@RequestBody ScoreByMoneyForm scoreByMoneyForm) {
        try {
            if(scoreByMoneyForm.getMoney() == null){
                return R.error("充值档位不能为空");
            }
           return R.ok().put("score",0);
        } catch (MyException e) {
            return R.error(e.getMessage());
        }
    }


    /**
     * V2.0获得个人的消费记录明细
     * @return
     */
    //@Login
    @ApiOperation("客户端消费记录")
    @GetMapping(value = "V2.0/history/get")
    public R getNewChargeHistory(@RequestParam Map<String, Object> map) {
        if (map.get("uid") == null) {
            return R.error("用户ID不能为空");
        }
        try {
            Page page = hyPayRecordService.findByNewPage(new QueryExt<>(map));
            return R.ok().put("page",page);
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 分页查询充值模板
     *
     * @param params
     * @return
     */
    @GetMapping("getList")
    @ApiOperation("分页查询充值模板")
    public R getList(@RequestParam Map<String, Object> params) {
        params.put("orderByField", "money");
        params.put("status", FrogConstant.BasicStatus.TRUE.getValue());
        Page page = chargeService.selectPageByConditions(new QueryExt(params));
        if (page.getRecords() != null) {
            List<HyCharge> list = page.getRecords();
            for (HyCharge charge :list){
                charge.setScore(0);
            }
            page.setRecords(list);
            return R.ok().put("page", page);
        } else {
            return R.error("获取失败");
        }
    }

    @ApiOperation("获取详情信息")
    @GetMapping("getDetail")
    public R getDetail(@RequestParam("id") Integer id) {
        System.out.println("ll");
        HyCharge hyCharge = chargeService.getById(id);
        HyChargeVO hyChargeVO = new HyChargeVO();
        if(hyCharge!=null){
            hyChargeVO.setPresent(hyCharge.getPresent());
            hyChargeVO.setTotal(hyCharge.getTotal());
        }
        return R.ok().put("hyChargeVO",hyChargeVO);
    }
}





